package com.gxa.service.impl;

import com.gxa.domain.entity.Permission;
import com.gxa.mapper.PermissionMapper;
import com.gxa.service.PermissionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限服务实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class PermissionServiceImpl implements PermissionService {
    
    private final PermissionMapper permissionMapper;
    
    @Override
    public List<Permission> getPermissionsByOperatorLevel(Integer operatorLevel) {
        log.info("获取操作员等级 {} 的权限列表", operatorLevel);
        return permissionMapper.getPermissionsByOperatorLevel(operatorLevel);
    }
    
    @Override
    public boolean hasPermission(Integer operatorLevel, String permissionName) {
        log.info("检查操作员等级 {} 是否有权限: {}", operatorLevel, permissionName);
        
        Permission permission = permissionMapper.getPermissionByName(permissionName);
        if (permission == null) {
            log.warn("权限 {} 不存在", permissionName);
            return false;
        }
        
        boolean hasPermission = false;
        if (operatorLevel == 1) {
            // 收银员权限
            hasPermission = permission.getCashierPermission() == 1;
        } else if (operatorLevel == 2 || operatorLevel == 3) {
            // 管理员权限（等级2和3）
            hasPermission = permission.getAdminPermission() == 1;
        }
        
        log.info("操作员等级 {} 权限 {} 检查结果: {}", operatorLevel, permissionName, hasPermission);
        return hasPermission;
    }
    
    @Override
    public Permission getPermissionByName(String permissionName) {
        return permissionMapper.getPermissionByName(permissionName);
    }
} 